<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Event/classes/PhpFox_ComponentUserEvents.class.php, $Author:  $ */



/** Component for listing user events
 * @version $Id: PhpFox_ComponentUserEvents.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.event
 *
 */
class PhpFox_Mod_Event_ComponentUserEvents extends BaseComponent
{
    function process()
    {
    	App::membersOnly();

        $oEventItem = $this->_oModule->getItem('Event');
        /* @var $oEventItem PhpFox_Mod_Event_ItemEvent */
        $oEventInviteItem = $this->_oModule->getItem('EventInvite');
        /* @var $oEventInviteItem PhpFox_Mod_Event_ItemEventInvite */
        $oEventService = $this->_oModule->getService('Event');
        /* @var $oEventService PhpFox_Mod_Event_ServiceEvent */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = $this->_oModule->getTemplate();
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        App::loadClass('Pager');

        $oSecurityService->checkPerm('events', array('read_only', 'full'));
        $oSecurityService->checkAccess('events');
        $sUser = $oSecurityService->getCurrentUserLogin();

        $sMode = '';
        $sMode = $oReq->get('mode');
        $aModes = array
        (
        	'ownevents',
        	'attended',
        	'invited',
        );
        if (!in_array($sMode, $aModes))
        {
            App::gotoUrl('public.event.main');
        }
        $iPage = $oReq->getInt('page', 1);
        if ($iInviteId = $oReq->getInt('delete'))
        {
            if (!$oEventInviteItem->load($iInviteId))
            {
                App::gotoUrl('public.event.main');
            }
            if ($oEventInviteItem->aData['user'] != $sUser)
            {
                App::gotoUrl('public.event.main');
            }
            $oEventInviteItem->delete($iInviteId);
            App::gotoUrl('public.event.uevent', array('mode' => 'invited', 'page' => $iPage));
        }
        $iPageSize = App::getParam('public.pagesize');
        $aCond = array();
        $aCond['user'] = '= "'.$sUser.'"';
        $sSort = 'ei.time DESC';
        $aFields = array
        (
        	'id'         => 'id',
        	'user'       => 'user',
        	'short'      => 'short',
        	'type'       => 'type',
            'city'       => 'city',
            'state'      => 'state',
            'country'    => 'country',
        	'title'      => 'title',
        	'start_time' => 'start_time',
        	'day'        => 'day',
        	'month'      => 'month',
        	'year'       => 'year',
        );
        if ($sMode == 'ownevents')
        {
            $sSort = 'e.time DESC';
            list($aItems, $iCnt) = $oEventItem->getList($aCond, $iPage, $iPageSize, $sSort, $aFields);
        }
        else
        {
            $aFields['invite_id'] = 'invite_id';
        	$aCond['invite'] = '= '.(($sMode == 'attended') ? 1 : 0);
        	$aCond['itype'] = '= 0';
        	list($aItems, $iCnt) = $oEventService->getEventInvites($aCond, $iPage, $iPageSize, $sSort, $aFields);
        }
        foreach ($aItems as $iKey => $aItem)
    	{
    	    $aItems[$iKey]['time'] = mktime(0, 0, 0, $aItem['month'], $aItem['day'], $aItem['year']);
    	}
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);

        $oTpl->assign(array(
        	'aItems'  => $aItems,
        	'sUser'   => $sUser,
        	'sMode'   => $sMode,
        	'aPaging' => ($iCnt) ? $oPager->getInfo($oUrl) : '',
        ));
        return $oTpl->fetch('UserEvents.html');
    }
}
?>